package 笔试.b站.空白.a;

import org.junit.Test;

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Solution {

     List<List<Integer>> res = new ArrayList<>();
     List<Integer> tmp = new ArrayList<>();

     int[] vis;
    int[] p;
    public String solution(int[] d) {
        p=d;
        vis=new int[p.length];
        for (int i = 1; i <= p.length ; i++) {
            qpl(i);
        }
        int m = -1;
        for (int i = 0; i < res.size(); i++) {
            int c = getNum(res.get(i));
            if(c%3==0 && c>m){
                m=c;
            }
        }
        if(m==-1)return "";
        return m+"";
    }

    private int getNum(List<Integer> t) {
        int c=0;
        for (int i = 0; i < t.size(); i++) {
            c=c*10+t.get(i);
        }
        return c;
    }

     void qpl(int mL) {
        if(mL==0){
            res.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < p.length; i++) {
            if(vis[i]==1)continue;
            tmp.add(p[i]); vis[i]=1; qpl(mL-1); vis[i]=0; tmp.remove(tmp.size()-1);
        }
    }
}

public class Main {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.solution(new int[]{1}));
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String res;

        int _d_size = 0;
        _d_size = Integer.parseInt(in.nextLine().trim());
        int[] _d = new int[_d_size];
        int _d_item;
        for(int _d_i = 0; _d_i < _d_size; _d_i++) {
            _d_item = Integer.parseInt(in.nextLine().trim());
            _d[_d_i] = _d_item;
        }

        res = new Solution().solution(_d);
        System.out.println(res);
    }
}
